#include "pthreadPool.h"

int initThreadPool(threadPool_t * pthreadPool,int workerNum){
    initTidArr(&pthreadPool->tidArr,workerNum);
    initQueue(&pthreadPool->taskQueue);
    pthread_mutex_init(&pthreadPool->mutex,NULL);
    pthread_cond_init(&pthreadPool->cond,NULL);
    printf("ThreadPool init OKay.\n");
    return 0;
}

int initTcp(){
    FILE *fp = fopen("./config/server.config","r");
    char ip[1024] = {0};
    char port[1024] = {0};
    fgets(ip,sizeof(ip),fp);
    char *p = strtok(ip," ");
    p = strtok(NULL," ");
    p = strtok(NULL," ");
    strcpy(ip,p);
    ip[strlen(ip)-1] = '\0';
    fgets(port,sizeof(port),fp);
    p = strtok(port," ");
    p = strtok(NULL," ");
    p = strtok(NULL," ");
    strcpy(port,p);
    port[strlen(port)-1] = '\0';
    /* printf("%s%s\n",ip1,port); */
    int sockFd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    int ret_con = connect(sockFd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret_con,-1,"connect");
    printf("TCP init OKay.\n");
    return sockFd;
}

int epollAdd(int epfd,int fd){
    struct epoll_event event;
    event.data.fd = fd;
    event.events = EPOLLIN;
    epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
    return 0;
}

int epollDel(int epfd,int fd){
    epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL);
    return 0;
}
